package com.example.gkcircle.fragment.borrow;

import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import com.example.gkcircle.App;
import com.example.gkcircle.BaseFragment;
import com.example.gkcircle.R;
import com.example.gkcircle.adapter.BorrowAdapter;
import com.example.gkcircle.library.LibraryRequest;
import com.example.gkcircle.library.object.Book;
import com.example.gkcircle.utils.ToastFactory;
import java.util.ArrayList;
import java.util.List;

/**
 * 借阅历史Fragment
 * @author ZhouHua
 * created at 15/12/21 上午12:57
 */
public class BorrowedFragment extends BaseFragment implements AbsListView.OnScrollListener{

    private ListView mListView;
    private SwipeRefreshLayout mSwipe;
    private View mHeaderView;
    private View mFooterView;
    private BorrowAdapter mAdapter;
    private int currentPage=1;
    private boolean isRefreshing;
    private static BorrowedFragment instance;

    public static BorrowedFragment getInstance() {
        if (instance == null) instance = new BorrowedFragment();
        return instance;
    }

    @Override
    protected int setLayout() {
        return R.layout.library_borrowed_fragment;
    }

    @Override
    protected void initView(View view) {
        mListView = (ListView) view.findViewById(R.id.library_borrowed_listview);
        mSwipe = (SwipeRefreshLayout) view.findViewById(R.id.library_borrowed_swipe);
        mSwipe.setColorSchemeColors(getResources().getColor(R.color.main), getResources().getColor(R.color.love), getResources().getColor(R.color.wish));
    }


    @Override
    protected void initListener() {
        mListView.setOnScrollListener(this);
        mSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (!isRefreshing) {
                    currentPage = 1;
                    isRefreshing = true;
                    getData();
                }
            }
        });
    }

    private void getData(){
        mSwipe.setRefreshing(true);
        LibraryRequest.getInstance().getBorrowed(currentPage,new LibraryRequest.IGetBorrowedListener() {
            @Override
            public void onSuccess(List<Book> book) {
                Message msg = new Message();
                if (book.size() > 0) {
                    if(App.mBorrowedList==null){
                        App.mBorrowedList = new ArrayList<Book>();
                    }else{
                        if(isRefreshing)App.mBorrowedList.clear();
                    }
                    for (Book tempBook : book) {
                        App.mBorrowedList.add(tempBook);
                    }
                    msg.what = 0;
                    handler.sendMessage(msg);
                } else {
                    msg.what = 1;
                    handler.sendMessage(msg);
                }
            }

            @Override
            public void onFailure(int code) {
                Message msg = new Message();
                msg.what = 2;
                handler.sendMessage(msg);
            }
        });
    }

    @Override
    protected void setData() {
        if(App.mBorrowedList!=null)return;
        getData();
    }


private Handler handler =new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what){
            case 0:
                isRefreshing=false;
                mSwipe.setRefreshing(false);
                if(!dataFlag)return;
                if(mAdapter!=null){
                    mAdapter.notifyDataSetChanged();
                    return;
                }
                //add headerView and footerView
                if(mHeaderView==null || mFooterView==null){
                    mHeaderView= LayoutInflater.from(getActivity()).inflate(R.layout.divider_header_footer, null);
                    mFooterView=LayoutInflater.from(getActivity()).inflate(R.layout.divider_header_footer, null);
                    mListView.addHeaderView(mHeaderView, null, false);
                    mListView.addFooterView(mFooterView, null, false);
                }
                mAdapter=new BorrowAdapter(getActivity(),App.mBorrowedList);
                mListView.setAdapter(mAdapter);
                break;
            case 1:
                isRefreshing=false;
                mSwipe.setRefreshing(false);
                ToastFactory.getToast(getActivity(), getResources().getString(R.string.no_more_data)).show();
                break;
            case 2:
                isRefreshing=false;
                mSwipe.setRefreshing(false);
                ToastFactory.getToast(getActivity(), getResources().getString(R.string.connection_error)).show();
                break;
            case 3:
                setData();
                break;
        }
    }
};

    @Override
    public void onPause() {
        mSwipe.setRefreshing(false);
        super.onPause();
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState) {
            case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                if (mListView.getLastVisiblePosition() == (mListView.getCount() - 1)) {
                    currentPage++;
                    getData();
                }
                break;
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }
}